package com.atomdayl.domain.therecipe.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Created by David on 2016/6/4.
 */
public class CheckNetWorkUtils {

    static boolean checkNetWork(Context context){
        boolean isAvailable = netWorkIsAvailable(context);
        if(!isAvailable){
            openDialog(context);
            return false;
        }
        return true;
    }

    public static boolean netWorkIsAvailable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if(activeNetInfo!=null){
            if(activeNetInfo.isAvailable()){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }

    public static boolean gpsWorkIsAvailable(Context context){
        LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }


    private static void openDialog(final Context context) {
        final AlertDialog.Builder builder=new AlertDialog.Builder(context);
        builder.setTitle("没有可用的网络");
        builder.setMessage("请开启GPRS或WIFI网络连接");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                Intent intent = new Intent("android.settings.WIRELESS_SETTINGS");
                context.startActivity(intent);


            }
        }).setNeutralButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.cancel();
            }
        }).create().show();

    }
}
